package com.github.wxiaoqi.security.biz.biz.pay;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.biz.biz.fightGroup.FightGroupInfoBiz;
import com.github.wxiaoqi.security.biz.biz.refund.RefundBiz;
import com.github.wxiaoqi.security.biz.biz.restaurant.RestaurantShoppingCartInfoBiz;
import com.github.wxiaoqi.security.biz.entity.CmsPayOrder;
import com.github.wxiaoqi.security.biz.entity.fightGroup.FightGroupOrder;
import com.github.wxiaoqi.security.biz.entity.restaurant.RestaurantOrderInfo;
import com.github.wxiaoqi.security.biz.feign.CrmFeign;
import com.github.wxiaoqi.security.biz.mapper.CmsPayOrderMapper;
import com.github.wxiaoqi.security.biz.mapper.FightGroupOrderMapper;
import com.github.wxiaoqi.security.biz.mapper.RestaurantOrderInfoMapper;

/**
 * 支付回调处理
 * @author he
 *
 */
@Service
public class PayCallBackBiz {

	private static final Logger logger = (Logger) LoggerFactory.getLogger(PayCallBackBiz.class);

	@Autowired
	private CrmFeign crmFeign;
	@Autowired
	private CmsPayOrderMapper cmsPayOrderMapper;
	@Autowired
	private RefundBiz refundBiz;
	@Autowired
	private FightGroupOrderMapper fightGroupOrderMapper;
	@Autowired
	private RestaurantOrderInfoMapper restaurantOrderInfoMapper;
	@Autowired
	private FightGroupInfoBiz fightGroupInfoBiz;
	@Autowired
	private RestaurantShoppingCartInfoBiz restaurantShoppingCartInfoBiz;

	/**
	 * 支付完成回调
	 * @param request
	 * @return
	 */
	public void payCallBack(Map<String, Object> params){
		logger.info("支付完成回调开始.......{}",params);
		String platformId = (String) params.get("platformId");
		String orderNo = (String) params.get("orderNo");
		String txStatus = (String) params.get("txStatus");
		String amount = (String) params.get("amount");
		BigDecimal amountDecimal = new BigDecimal(amount);
		CmsPayOrder cmsScenesOrder = new CmsPayOrder();
		cmsScenesOrder.setPlatformId(platformId);
		cmsScenesOrder.setPayOrderNo(orderNo);
		CmsPayOrder selectCmsPayOrder = cmsPayOrderMapper.selectOne(cmsScenesOrder);
		if(null == selectCmsPayOrder){
			logger.info("支付完成回调-订单不存在.......");
			return;
		}
		if(0 != selectCmsPayOrder.getTxAmt().compareTo(amountDecimal)){
			logger.info("支付完成回调-金额不一致.......");
			return;
		}

		//支付成功入账，更新订单状态
		if("00".equals(txStatus)){
			crmFeign.inAccounting(params);
			selectCmsPayOrder.setTxStatus("00");//支付成功
		}else{
			selectCmsPayOrder.setTxStatus("01");//支付失败
		}
		cmsPayOrderMapper.updateByPrimaryKey(selectCmsPayOrder);

		//更新场景订单
		String scenesCode = selectCmsPayOrder.getScenesCode();
		params.put("scenesCode", scenesCode);
		if("4".equals(scenesCode)){
			//竞拍 不存在第三方支付
		}else if("5".equals(scenesCode) || "6".equals(scenesCode)){
			//拼团参团//拼团创建团
			this.updateFightGroupOrder(params, selectCmsPayOrder);
		}else if("3".equals(scenesCode) || "2".equals(scenesCode) || "1".equals(scenesCode)){
			//店内点餐//预订点餐//外卖
			this.updateOrderDishesOrder(params, selectCmsPayOrder);
		}
	}

	/**
	 * 拼团订单处理
	 * @param params
	 */
	private void updateFightGroupOrder(Map<String, Object> params, CmsPayOrder cmsScenesOrder){
		//根据订单号查询竞拍订单
		FightGroupOrder fightGroupOrder = new FightGroupOrder();
		fightGroupOrder.setPlatformId((String) params.get("platformId"));
		fightGroupOrder.setPayOrderNo((String) params.get("orderNo"));
		FightGroupOrder selectFightGroupOrder = fightGroupOrderMapper.selectOne(fightGroupOrder);
		if(null != selectFightGroupOrder){
			if("00".equals((String) params.get("txStatus"))){
				selectFightGroupOrder.setStatus("00");//支付成功
			}else{
				selectFightGroupOrder.setStatus("01");//支付失败
			}
			fightGroupOrderMapper.updateByPrimaryKey(selectFightGroupOrder);
		}
		//处理业务逻辑
		String extMap = cmsScenesOrder.getExtMap();
		JSONObject jsonExtMap = (JSONObject) JSONObject.parse(extMap);
		Map<String, Object> request = new HashMap<String, Object>();
		request.put("platformId", (String) params.get("platformId"));
		request.put("merchantId", cmsScenesOrder.getMerchantId());
		request.put("customerId", cmsScenesOrder.getCustomerId());
		request.put("goodsId", jsonExtMap.get("goodsId"));
		request.put("name", jsonExtMap.get("nickName"));
		request.put("userHeadPortrait", jsonExtMap.get("avatarUrl"));
		if("5".equals((String) params.get("scenesCode"))){//拼团参团
			request.put("groupId", jsonExtMap.get("groupId"));
			fightGroupInfoBiz.joinGroup(request);
		}else if("6".equals((String) params.get("scenesCode"))){//拼团创建团
			request.put("groupsId", jsonExtMap.get("groupsId"));
			request.put("payOrderNo", cmsScenesOrder.getPayOrderNo());
			fightGroupInfoBiz.createGroup(request);
		}
	}
	
	/**
	 * 点餐订单处理
	 * @param params
	 */
	private void updateOrderDishesOrder(Map<String, Object> params, CmsPayOrder cmsScenesOrder){
		RestaurantOrderInfo restaurantOrderInfo = new RestaurantOrderInfo();
		restaurantOrderInfo.setPlatformId((String) params.get("platformId"));
		restaurantOrderInfo.setPayOrderNo((String) params.get("orderNo"));
		RestaurantOrderInfo selectRestaurantOrderInfo = restaurantOrderInfoMapper.selectOne(restaurantOrderInfo);
		if(null != selectRestaurantOrderInfo){
			if("00".equals((String) params.get("txStatus"))){
				selectRestaurantOrderInfo.setStatus("00");//支付成功
			}else{
				selectRestaurantOrderInfo.setStatus("01");//支付失败
			}
			restaurantOrderInfoMapper.updateByPrimaryKey(selectRestaurantOrderInfo);
		}
		//处理业务逻辑
		String extMap = cmsScenesOrder.getExtMap();
		JSONObject jsonExtMap = (JSONObject) JSONObject.parse(extMap);
		Map<String, Object> request = new HashMap<String, Object>();
		request.put("platformId", (String) params.get("platformId"));
		request.put("merchantId", cmsScenesOrder.getMerchantId());
		if("3".equals((String) params.get("scenesCode"))){//店内点餐
			request.put("tableId", jsonExtMap.get("tableId"));
			restaurantShoppingCartInfoBiz.orderPayUpdate(request);
		}else if("2".equals((String) params.get("scenesCode"))){//预订点餐
			request.put("customerId", cmsScenesOrder.getCustomerId());
			request.put("type", "3");
			restaurantShoppingCartInfoBiz.reservationPayUpdate(request);
		}else if("1".equals((String) params.get("scenesCode"))){//外卖
			request.put("customerId", cmsScenesOrder.getCustomerId());
			request.put("type", "2");
			restaurantShoppingCartInfoBiz.reservationPayUpdate(request);
		}
	}

}
